#include <gtest/gtest.h>
#include <gdstk/gdstk.hpp>
#include <iostream>
#include "qlib/all.h"
#include "qlib/tests/db_test/check.h"

using namespace qlib;

Library::s_ptr qlib_library;
gdstk::Library gdstk_library;

ErrorCode qlib_err;
gdstk::ErrorCode gdstk_err;

std::string fileName = "E:/T/gds/138bit_line12-T.gds";
bool continue_check = false;

// gds文件读取正确性测试
// TEST(ReadGdsTest, BasicAssertion) {
//   qlib_err = ErrorCode::NoError;
//   gdstk_err = gdstk::ErrorCode::NoError;

//   qlib_library = DB::read_gds(fileName.c_str(), 1e-9, 0.01, &qlib_err);
//   EXPECT_EQ((int)(qlib_err), 0);

//   gdstk_library = gdstk::read_gds(fileName.c_str(), 1e-9, 0.01, nullptr, &gdstk_err);
//   EXPECT_EQ((int)(gdstk_err), 0);

//   // Librar整体校验
//   // continue_check = !Check::check_library(qlib_library, &gdstk_library);
//   // EXPECT_EQ(continue_check, false);

//   // // Library部分校验
//   // if (continue_check) {
//   //   // 基本信息
//   //   EXPECT_EQ(Check::compare_string(qlib_library->name, gdstk_library.name), true);
//   //   EXPECT_EQ(qlib_library->unit == gdstk_library.unit, true);
//   //   EXPECT_EQ(qlib_library->precision == gdstk_library.precision, true);

//   //   // 属性
//   //   EXPECT_EQ(Check::check_property(qlib_library->properties, *gdstk_library.properties), true);

//   //   // cell
//   //   ASSERT_EQ(qlib_library->cells.size() == gdstk_library.cell_array.count, true);

//   //   for (size_t i = 0; i < qlib_library->cells.size(); i++) {
//   //     EXPECT_EQ(Check::check_cell(qlib_library->cells[i], gdstk_library.cell_array[i]), true);
//   //   }
//   // }
// }

// // gds文件写入测试
// TEST(WriteGdsTest, BasicAssertion) {
//   tm time;
//   gdstk::get_now(time);

//   qlib_err = DB::write_gds(qlib_library, "E:/T/gds/out/138bit_line12-T.gds", 199, &time);
//   EXPECT_EQ((int)(qlib_err), 0);

//   // gdstk::Library gdstk_library_t = gdstk::read_gds("E:/T/gds/out/138bit_line12-T.gds", 1e-9, 0.01, nullptr, &gdstk_err);
//   // EXPECT_EQ((int)(gdstk_err), 0);

//   // // Librar整体校验
//   // continue_check = !Check::check_library(qlib_library, &gdstk_library_t);
//   // EXPECT_EQ(continue_check, false);

//   // // Library部分校验
//   // if (continue_check) {
//   //   // 基本信息
//   //   EXPECT_EQ(Check::compare_string(qlib_library->name, gdstk_library_t.name), true);
//   //   EXPECT_EQ(qlib_library->unit == gdstk_library_t.unit, true);
//   //   EXPECT_EQ(qlib_library->precision == gdstk_library_t.precision, true);

//   //   // 属性
//   //   EXPECT_EQ(Check::check_property(qlib_library->properties, *gdstk_library_t.properties), true);

//   //   // cell
//   //   ASSERT_EQ(qlib_library->cells.size() == gdstk_library_t.cell_array.count, true);

//   //   for (size_t i = 0; i < qlib_library->cells.size(); i++) {
//   //     EXPECT_EQ(Check::check_cell(qlib_library->cells[i], gdstk_library.cell_array[i]), true);
//   //   }
//   // }
// }

// gds文件读取性能测试-qlib
TEST(ReadGdsTimeTest_qlib, BasicAssertion) {
  qlib_err = ErrorCode::NoError;

  qlib_library = DB::read_gds(fileName.c_str(), 1e-9, 0.01, &qlib_err);
  EXPECT_EQ((int)(qlib_err), 0);
}

// gds文件读取性能测试-gdstk
TEST(ReadGdsTimeTest_gdstk, BasicAssertion) {
  gdstk::ErrorCode gdstk_err = gdstk::ErrorCode::NoError;

  gdstk_library = gdstk::read_gds(fileName.c_str(), 1e-9, 0.01, nullptr, &gdstk_err);
  EXPECT_EQ((int)(gdstk_err), 0);
}

// gds文件写出性能测试-qlib
TEST(WriteGdsTimeTest_qlib, BasicAssertion) {
  tm time;
  gdstk::get_now(time);

  qlib_err = DB::write_gds(qlib_library, "E:/T/gds/out/output-qlib.gds", 199, &time);
  EXPECT_EQ((int)(qlib_err), 0);
}

// gds文件写出性能测试-gdstk
TEST(WriteGdsTimeTest_gdstk, BasicAssertion) {
  tm time;
  gdstk::get_now(time);

  gdstk_library.write_gds("E:/T/gds/out/outout-gdstk.gds", 199, &time);
  EXPECT_EQ((int)(qlib_err), 0);
}